//
//  ST_DTImageChatCell.swift
//  DrinkTogether
//
//  Created by DouLei on 2018/6/25.
//  Copyright © 2018 DouLei. All rights reserved.
//

import UIKit
import SwifterSwift
import SDWebImage


class ST_DTImageChatCell: ST_DTChatCell {
    
    
    fileprivate lazy var chat_image_view:UIButton = {
        
        let control = UIButton.init(type: UIButton.ButtonType.custom)
        control.add_touch_up(insideTarget: self, action: #selector(display_full_image))
        //        control.backgroundColor = Color.random
        control.cornerRadius = 15
        control.imageView?.contentMode = .scaleAspectFill
        return control
    }()
    
    @objc func display_full_image(){
        
        
        if let check = message.passedCheck , check == false{
            return
        }
        
        if let vc = self.parentViewController as? ST_PrivateChatViewController{
            vc.dismissMessageToolBar()
        }
        
        self.show_full_image_closure?(self)
    }
    
    var show_full_image_closure:((ST_DTImageChatCell)->())?
    
    fileprivate lazy var uploading_mas_view:UIView = {
        let control = UIView()
        control.backgroundColor = Color.gray
        control.alpha = 0.8
        control.cornerRadius = 15
        control.isHidden = true
        return control
    }()
    
    fileprivate lazy var indicator:UIActivityIndicatorView = {
        
        let control = UIActivityIndicatorView()
        
        return control
        
    }()
    fileprivate lazy var indicator_label:UILabel = {
        
        let control = UILabel()
        control.textColor = Color.white
        control.font = UIFont.systemFont(ofSize: 12)
        control.textAlignment = .center
        return control
    }()
    
    fileprivate lazy var thumb_up_image_view:UIImageView = {
        let control = UIImageView.init(image: ST_ImageUtils.imageName("消息_icon_配对点赞")!)
        control.isHidden = true
        return control
    }()
    
    fileprivate lazy var close_mask_view:UIView = {
        let control = UIView()
        control.backgroundColor = UIColor.init(hex: 0x373B40, transparency: 0.9)
        control.isHidden = true
        control.cornerRadius = 15
        return control
    }()
    
    fileprivate lazy var close_status_icon_view:UIImageView = {
        let control = UIImageView.init(image: ST_ImageUtils.imageName("snapChat_close_icon"))
        control.contentMode = .scaleAspectFill
        return control
    }()
    
    fileprivate lazy var close_title_label:UILabel = {
        let control = UILabel.init()
        control.text = "图片违规"
        control.textColor = UIColor.white
        control.textAlignment = .center
        control.font = UIFont.systemFont(ofSize: 14, weight: UIFont.Weight.regular)
        return control
    }()
    
    override func commonInit(){
        
        super.commonInit()
        
        self.contentView.addSubview(chat_image_view)
        
        self.contentView.addSubview(uploading_mas_view)
        
        
        self.uploading_mas_view.addSubview(indicator)
        self.uploading_mas_view.addSubview(indicator_label)
        
        indicator.snp.makeConstraints { (make) in
            make.width.height.equalTo(44)
            make.center.equalToSuperview()
        }
        
        indicator_label.snp.makeConstraints { (make) in
            make.centerX.equalToSuperview()
            make.top.equalTo(indicator.snp.bottom).offset(-20+5)
        }
        
        contentView.addSubview(close_mask_view)
        close_mask_view.snp.makeConstraints { (make) in
            make.edges.equalTo(chat_image_view)
        }
        
        close_mask_view.addSubview(close_status_icon_view)
        close_status_icon_view.snp.makeConstraints { (make) in
            make.centerX.equalToSuperview()
            make.top.equalToSuperview().offset(43)
            make.width.height.equalTo(30)
        }
        
        
        close_mask_view.addSubview(close_title_label)
        close_title_label.snp.makeConstraints { (make) in
            make.centerX.equalToSuperview()
            make.top.equalToSuperview().offset(78)
            make.height.equalTo(20)
        }
        
        
        
        self.contentView.addSubview(thumb_up_image_view)
        
    }
    
    
    override var message: ST_DTChatMessage!{
        didSet{
            
            
            self.uuid = message.uuid
            
            icon.pin_setImage(from: message.portraitUri.compress_image().url, placeholderImage: ST_SwiftConst.shared.fetch_random_place_holder_image())
            
            chat_image_view.snp.updateConstraints { (make) in
                make.width.equalTo(message.imageFrame.width)
                make.height.equalTo(message.imageFrame.height)
            }
            
            
            
            thumb_up_image_view.isHidden = !message.displayThumbUp
            
            if let check = message.passedCheck ,check == false{
                
                close_mask_view.isHidden = false
                
                chat_image_view.sd_setAllImage(with: "", placeholderImageName: nil)
                
                chat_image_view.imageView?.image = nil
                
            }else{
                close_mask_view.isHidden = true
                //                判断 本地有没有这个图片
                if let localPath = message.thumbnailImageLocalPath,FileManager.default.fileExists(atPath: localPath),let localImage = localPath.load_image_from_disk(){
                    
                    [UIControl.State.normal, UIControl.State.selected, UIControl.State.highlighted, UIControl.State.disabled].forEach {
                        
                        chat_image_view.sd_setImage(with: nil, for: $0, placeholderImage: localImage, options: SDWebImageOptions.init(), completed: nil)
                        
                    }
                    
                }else {
                    
                    chat_image_view.sd_setAllImage(with: message.imageRemoteUrl ?? "", placeholderImageName: nil)
                    
                }
            }
            
            
        }
    }
    
    
    
    
    override func setting_uplaod_status(){
        
        if message.uploadStatus == .success{
            uploading_mas_view.isHidden = true
            
            indicator.stopAnimating()
            //                    print("A9999success")
        }else if message.uploadStatus == .failure{
            uploading_mas_view.isHidden = true
            
            
            indicator.stopAnimating()
            
        }else{
            
            uploading_mas_view.isHidden = false
            
            indicator.startAnimating()
            
            indicator_label.text = "\(message.uploadImageProgress)%"
        }
        
        
        //        print("当前进度条:",message.uploadImageProgress)
        
    }
    
    
    
}









//MARK: - LeftCell

class ST_DTImageChatLeftCell: ST_DTImageChatCell {
    
    override func commonInit() {
        super.commonInit()
        
        icon.snp.makeConstraints { (make) in
            make.width.height.equalTo(40)
            make.left.equalToSuperview().offset(15)
            make.top.equalToSuperview().offset(15)
        }
        
        chat_image_view.snp.makeConstraints { (make) in
            make.height.equalTo(100)
            make.width.equalTo(100)
            make.left.equalTo(icon.snp.right).offset(10)
            make.top.equalTo(icon)
        }
        
        thumb_up_image_view.snp.makeConstraints { (make) in
            make.left.equalTo(chat_image_view.snp.right).offset(-20)
            make.top.equalTo(chat_image_view)
            make.height.width.equalTo(40)
        }
        
        uploading_mas_view.snp.makeConstraints { (make) in
            make.edges.equalTo(chat_image_view)
        }
        
        //        upload_error_btn.snp.makeConstraints { (make) in
        //            make.left.equalTo(chat_image_view.snp.right).offset(10)
        //            make.centerY.equalTo(chat_image_view)
        //            make.width.height.equalTo(ST_DTChatCell.uploadErrorBtnSize)
        //        }
        
        self.contentView.addSubview(tailView)
        tailView.snp.makeConstraints { (make) in
            make.height.equalTo(17)
            make.top.equalTo(chat_image_view.snp.bottom).offset(5)
            make.left.equalTo(chat_image_view)
        }
        
    }
    
    fileprivate lazy var tailView:ST_ChatMessageTailControl = {
        let control = ST_ChatMessageTailControl()
        return control
    }()
    
    override var message: ST_DTChatMessage!{
        didSet{
            
            tailView.model = self.message.tail
            
        }
    }
}




//MARK:-Right Cell
class ST_DTImageChatRightCell: ST_DTImageChatCell {
    
    override func commonInit() {
        super.commonInit()
        
        icon.removeFromSuperview()
        
        chat_image_view.snp.makeConstraints { (make) in
            make.height.equalTo(100)
            make.width.equalTo(100)
            make.right.equalToSuperview().offset(-30)
            make.top.equalToSuperview().offset(15)
        }
        
        thumb_up_image_view.snp.makeConstraints { (make) in
            make.right.equalTo(chat_image_view.snp.left).offset(20)
            make.top.equalTo(chat_image_view)
            make.height.width.equalTo(40)
        }
        
        uploading_mas_view.snp.makeConstraints { (make) in
            make.edges.equalTo(chat_image_view)
        }
        
        
        contentView.addSubview(upload_error_btn)
        upload_error_btn.snp.makeConstraints { (make) in
            make.right.equalTo(chat_image_view.snp.left).offset(-10)
            make.centerY.equalTo(chat_image_view)
            make.width.height.equalTo(ST_DTChatCell.uploadErrorBtnSize)
        }
        
        
        
        contentView.addSubview(uploadIndicator)
        uploadIndicator.snp.makeConstraints { (make) in
            make.centerY.equalTo(chat_image_view)
            make.right.equalTo(chat_image_view.snp.left).offset(-8)
        }
    }
    
    
    override func setting_uplaod_status() {
        
        super.setting_uplaod_status()
        
        if message.uploadStatus == .uploading {
            
            uploadIndicator.startAnimating()
            
        }else{
            uploadIndicator.stopAnimating()
        }
        
        upload_error_btn.isHidden = message.uploadStatus != .failure
        
        print("【聊天蒙层问题】---uploading_mas_view.isHidden = ",uploading_mas_view.isHidden)
        
        
    }
}
